// import type { CostGuard, CostGuards } from './profile';
// import { PoolObject } from './Pool';
// import { runtimeError } from './log';
// import type { msgData } from './Utils';

type EventCallbackFunc = (...args: any[]) => unknown;

class CallObject extends PoolObject {
	call?: EventCallbackFunc;

	self: unknown;

	called = 0;

	guard: CostGuard;

	initCall(name: string, call: EventCallbackFunc, self: unknown, guards: CostGuards): void {
		this._name = name;
		this.call = call;
		this.called = 0;
		this.self = self;
		this.guard = guards.findGuard(name);
	}

	onCallTimer(...args: any[]): boolean {
		return this.onCallEvent(args) as boolean;
	}

	onCallEvent(args: msgData): unknown {
		const rt = this.guard.tryCall(this.call, this.self, args);
		this.called++;
		return rt;
	}

	release(): void {
		if (this.guard) {
			this.guard = undefined;
			super.release();
		} else runtimeError(`callObject.release twice.${this._name}`);
	}
}
